﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace Main
{
    internal class SelectionAdorner : Adorner
    {
        public SelectionAdorner(UIElement adornedEIeent) : base(adornedEIeent) { }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            Rect adornerRect = new Rect(AdornedElement.DesiredSize);
            SolidColorBrush renderBrush = Brushes.Transparent;
            Pen render = new Pen(new SolidColorBrush(Colors.OrangeRed), 1);
            drawingContext.DrawRectangle(renderBrush, render, new Rect(adornerRect.TopLeft.X, adornerRect.TopLeft.Y, adornerRect.Width, adornerRect.Height));

            MouseDown += SelectionAdorner_MouseDown;
            MouseMove += SelectionAdorner_MouseMove;
            MouseUp += SelectionAdorner_MouseUp;
        }

        private void SelectionAdorner_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ReleaseMouseCapture();
            CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ClearAlignLine();
        }

        Point lastPoint = new Point();
        private void SelectionAdorner_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                CaptureMouse();
                var nowPoint = e.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
                int offsetX = (int)(nowPoint.X - lastPoint.X);
                int offsetY = (int)(nowPoint.Y - lastPoint.Y);

                CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).MoveControls(offsetX, offsetY);
                lastPoint = nowPoint;
            }
            else if (e.RightButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                CaptureMouse();
                var nowPoint = e.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
                int offsetX = (int)(nowPoint.X - lastPoint.X);
                int offsetY = (int)(nowPoint.Y - lastPoint.Y);

                CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ZoomControls(offsetX, offsetY);
                lastPoint = nowPoint;
            }
        }

        private void SelectionAdorner_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lastPoint = e.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
        }
    }

}
